package cn.ctodb.cms.web.rest;

import com.codahale.metrics.annotation.Timed;
import cn.ctodb.cms.service.CmsTagService;
import cn.ctodb.cms.web.rest.util.HeaderUtil;
import cn.ctodb.cms.service.dto.CmsTagDTO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing CmsTag.
 */
@RestController
@RequestMapping("/api")
public class CmsTagResource {

    private final Logger log = LoggerFactory.getLogger(CmsTagResource.class);
        
    @Inject
    private CmsTagService cmsTagService;

    /**
     * POST  /cms-tags : Create a new cmsTag.
     *
     * @param cmsTagDTO the cmsTagDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new cmsTagDTO, or with status 400 (Bad Request) if the cmsTag has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/cms-tags")
    @Timed
    public ResponseEntity<CmsTagDTO> createCmsTag(@RequestBody CmsTagDTO cmsTagDTO) throws URISyntaxException {
        log.debug("REST request to save CmsTag : {}", cmsTagDTO);
        if (cmsTagDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("cmsTag", "idexists", "A new cmsTag cannot already have an ID")).body(null);
        }
        CmsTagDTO result = cmsTagService.save(cmsTagDTO);
        return ResponseEntity.created(new URI("/api/cms-tags/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert("cmsTag", result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /cms-tags : Updates an existing cmsTag.
     *
     * @param cmsTagDTO the cmsTagDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated cmsTagDTO,
     * or with status 400 (Bad Request) if the cmsTagDTO is not valid,
     * or with status 500 (Internal Server Error) if the cmsTagDTO couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/cms-tags")
    @Timed
    public ResponseEntity<CmsTagDTO> updateCmsTag(@RequestBody CmsTagDTO cmsTagDTO) throws URISyntaxException {
        log.debug("REST request to update CmsTag : {}", cmsTagDTO);
        if (cmsTagDTO.getId() == null) {
            return createCmsTag(cmsTagDTO);
        }
        CmsTagDTO result = cmsTagService.save(cmsTagDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert("cmsTag", cmsTagDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /cms-tags : get all the cmsTags.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of cmsTags in body
     */
    @GetMapping("/cms-tags")
    @Timed
    public List<CmsTagDTO> getAllCmsTags() {
        log.debug("REST request to get all CmsTags");
        return cmsTagService.findAll();
    }

    /**
     * GET  /cms-tags/:id : get the "id" cmsTag.
     *
     * @param id the id of the cmsTagDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the cmsTagDTO, or with status 404 (Not Found)
     */
    @GetMapping("/cms-tags/{id}")
    @Timed
    public ResponseEntity<CmsTagDTO> getCmsTag(@PathVariable Long id) {
        log.debug("REST request to get CmsTag : {}", id);
        CmsTagDTO cmsTagDTO = cmsTagService.findOne(id);
        return Optional.ofNullable(cmsTagDTO)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * DELETE  /cms-tags/:id : delete the "id" cmsTag.
     *
     * @param id the id of the cmsTagDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/cms-tags/{id}")
    @Timed
    public ResponseEntity<Void> deleteCmsTag(@PathVariable Long id) {
        log.debug("REST request to delete CmsTag : {}", id);
        cmsTagService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("cmsTag", id.toString())).build();
    }

    /**
     * SEARCH  /_search/cms-tags?query=:query : search for the cmsTag corresponding
     * to the query.
     *
     * @param query the query of the cmsTag search 
     * @return the result of the search
     */
    @GetMapping("/_search/cms-tags")
    @Timed
    public List<CmsTagDTO> searchCmsTags(@RequestParam String query) {
        log.debug("REST request to search CmsTags for query {}", query);
        return cmsTagService.search(query);
    }


}
